// 管理用户相关信息的 store
import {defineStore} from 'pinia'
import {reqUserLogin} from '@/api/user.js'
import {ElMessage} from "element-plus";
// import {ElMessage} from "element-plus";

export const useUserStore = defineStore('User', {
    state: () => {
        return {
            token: "",
            userInfo:{},
        }
    },
    actions: {
        async gotoLogin({account,password}) {
            const res = await reqUserLogin({account,password})
            if (res.data.code === "1") {
                this.userInfo = res.data.result
                this.token = res.data.result.token
                localStorage.setItem("XTX-USERINFO",JSON.stringify(this.userInfo))
                console.log(this.userInfo)
                ElMessage.success("登录成功")
            } else {
                ElMessage.error("登录失败")
            }
        },
        // 退出登录
        handleLogout() {
            this.userInfo = {}
            localStorage.removeItem("XTX-USERINFO")
            ElMessage.success("退出登录成功")
        },
        // 获取用户信息
        getUserInfo() {
            const userInfo = localStorage.getItem("XTX-USERINFO")
            if (userInfo) {
                this.userInfo = JSON.parse(userInfo)
            }
            return this.userInfo
        },
    },
})